﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FYP14
{
    public partial class AddSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAllDoctors();
                SetInitialRow();
                if (Request.QueryString["ID"] != null)
                {
                    SetValues(Convert.ToInt32(Request.QueryString["ID"]));
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Schedule oSchedule = new Schedule();
            oSchedule.Doctor_id = Convert.ToInt32(ddldoctor.SelectedValue);
            oSchedule.Schedule_Name = txtScheduleName.Text;

            if (Request.QueryString["ID"] != null)
            {
                oSchedule.Schedule_id = Convert.ToInt32(Request.QueryString["ID"]);
                BAL.Updateschedule(oSchedule);
            }
            else
            {
                BAL.SaveSchedule(oSchedule);
            }

            Response.Redirect("Viewschedule.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void SetValues(Int32 ID)
        {
            DataTable dt = BAL.GetSchedule(ID);
            if (dt.Rows.Count > 0)
            {
                ddldoctor.Items[ddldoctor.Items.IndexOf(ddldoctor.Items.FindByValue(dt.Rows[0]["doctor_id"].ToString()))].Selected = true;
                txtScheduleName.Text = dt.Rows[0]["schedule_name"].ToString();
            }
        }

        private void GetAllDoctors()
        {
            DataTable dt = BAL.GetAllDoctors();
            ddldoctor.DataValueField = "doctor_id";
            ddldoctor.DataTextField = "name";
            ddldoctor.DataSource = dt;
            ddldoctor.DataBind();
        }

        private void SetInitialRow()
        {
            DataTable dt = BAL.GetScheduleDetailsFroScheduleID(Convert.ToInt32(Request.QueryString["ID"]));
            ViewState["Savedt"] = dt;
            gvPurchase.DataSource = dt;
            gvPurchase.DataBind();
        }

        protected void gvPurchase_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }
        }

        protected void lnkRemove_Click(object sender, EventArgs e)
        {
            ImageButton lb = (ImageButton)sender;
            ViewState["UpdatedID"] = lb.CommandArgument;
            DataTable dt = BAL.GetScheduleDetail(Convert.ToInt32(lb.CommandArgument));
            if (dt.Rows.Count > 0)
            {
                ddlDayOfWeek.ClearSelection();
                ddlDayOfWeek.Items[ddlDayOfWeek.Items.IndexOf(ddlDayOfWeek.Items.FindByValue(dt.Rows[0]["day_of_week"].ToString()))].Selected = true;
                ((TextBox)(dtcdate.FindControl("TextBox1"))).Text = dt.Rows[0]["from_time"].ToString();
                ((TextBox)(dtcTodate.FindControl("TextBox11"))).Text = dt.Rows[0]["to_time"].ToString();
                string radalertscript = "<script language='javascript'>ShowHideDetailsection();window.scrollTo(0,document.body.scrollHeight);</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
            }
        }

        protected void gvPurchase_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPurchase.PageIndex = e.NewPageIndex;
            DataTable dt = new DataTable();
            if (ViewState["Savedt"] != null)
            {
                dt = (DataTable)ViewState["Savedt"];
            }
            else
            {
                dt = BAL.GetScheduleDetailsFroScheduleID(Convert.ToInt32(Request.QueryString["ID"]));
                ViewState["Savedt"] = dt;
            }
            gvPurchase.DataSource = dt;
            gvPurchase.DataBind();
        }

        protected void btnSaveDetail_Click(object sender, EventArgs e)
        {
            ScheduleDetail oSchedule = new ScheduleDetail();
            oSchedule.Schedule_id = Convert.ToInt32(Request.QueryString["ID"].ToString());
            oSchedule.DayOfWeek = Convert.ToInt32(ddlDayOfWeek.SelectedValue);
            oSchedule.from_time = Convert.ToDateTime(((TextBox)(dtcdate.FindControl("TextBox1"))).Text);
            oSchedule.to_time = Convert.ToDateTime(((TextBox)(dtcTodate.FindControl("TextBox11"))).Text);

            if (ViewState["UpdatedID"] != null)
            {
                oSchedule.Schedule_Detail_id = Convert.ToInt32(ViewState["UpdatedID"]);
                BAL.UpdatescheduleDetail(oSchedule);
                ViewState.Remove("UpdatedID");
            }
            else
            {
                BAL.SaveScheduleDetails(oSchedule);
            }
            SetInitialRow();

            string radalertscript = "<script language='javascript'>ShowHideDetailsection();window.scrollTo(0,document.body.scrollHeight);</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
        }
    }
}